﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ConfigEditor
{
    public partial class Form1 : eTMS.Controls.BaseForm
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void ChangeServer(string fileName, string replace)
        {
            string search = "net.tcp://";
            int i = 0;
            string content = File.ReadAllText(fileName);
            do
            {
                i = content.IndexOf(search, i);
                if (i > 0)
                {
                    int i1 = content.IndexOf("/", i + search.Length + 1);
                    if (i1 > 0)
                    {
                        content = content.Substring(0, i + search.Length) + replace + content.Substring(i1);
                        i = i + replace.Length + search.Length;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            while (true);
            File.WriteAllText(fileName, content);
        }
        private void GetCurrentConfig()
        {
            string dir = Application.StartupPath;
            string fileName=dir+"\\eTMS.exe.config";
            string search = "net.tcp://";
            int i = 0;
            string content = File.ReadAllText(fileName);
            do
            {
                i = content.IndexOf(search, i);
                if (i > 0)
                {
                    int i1 = content.IndexOf("/", i + search.Length + 1);
                    if (i1 > 0)
                    {
                        content = content.Substring(i + search.Length,i1-i-search.Length);
                        string[] arr = content.Split(':');
                        if (arr.Length == 2)
                        {
                            txtServer.Text = arr[0];
                            txtPort.Text = arr[1];
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            } while (true);
        }
        private void ucCommandDialog1_OnOK(object sender, CancelEventArgs e)
        {
            int port = int.Parse(txtPort.Text);
            string st = txtServer.Text + ":" + txtPort.Text;
            string dir = Application.StartupPath;
            ChangeServer(dir + "\\eTMS.exe.config", st);
            st = txtServer.Text + ":" + Convert.ToString(port + 1);
            ChangeServer(dir + "\\AutoUpdate\\eTMSUpdateService.exe.config", st);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetCurrentConfig();
        }
    }
}
